package com.devotion.velocity.util;

import com.devotion.velocity.annotation.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public class AnnotationUtil {

    public static boolean isComponent(Class clazz) {
        return clazz.isAnnotationPresent(Component.class);
    }

    public static boolean matchType(Class clazz, Class<? extends Annotation> annotationType) {
        return clazz.isAnnotationPresent(annotationType);
    }

    public static Object getValue(Class clazz, Class annotationType, String name) {
        Object value = null;
        try {
            Annotation annotation = clazz.getAnnotation(annotationType);
            if (annotation != null) {
                Method m = annotation.getClass().getDeclaredMethod(name, null);
                if (m != null) {
                    value = m.invoke(annotation);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

}
